<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="db.jsp"%>
<html>
<head>
    <title>JDBC</title>
    <script>
        function deleteOne(id) {
            if (confirm("你确定要删除这个学生吗？")) {
                window.location = "student.jsp?action=delete&id=" + id;
            }
        }

        function checkAll() {
            var checkAll = document.getElementById("checkAll");
            var list = document.getElementsByClassName("checkIds");
            for (var i = 0; i < list.length; i++) {
                list[i].checked = checkAll.checked;
            }
        }

        function deleteMany() {
            var ids = [];
            var list = document.getElementsByClassName("checkIds");
            for (var i = 0; i < list.length; i++) {
                if (list[i].checked) {
                    ids.push(list[i].value);
                }
            }
            if (ids.length > 0) {
                if (confirm("你确定要删除这" + ids.length + "个学生吗？")) {
                    window.location = "student.jsp?action=delete&id=" + ids.join(",");
                }

            } else {
                alert("请选中一个学生删除");
            }


        }


        function clear() {
            document.getElementById("sno").value = "";
            document.getElementById("name").value = "";
            document.getElementById("phone").value = "";
        }
    </script>

    <style>
        span{
            color: red;
            font-weight: bold;
            font-size: large;
        }
    </style>
</head>
<body>

<%!
    public static boolean isEmpty(String s) {
        return s == null || s.length() == 0;
    }
%>
<%
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");

    Object oname=session.getAttribute("name");
    if(oname==null){
        request.setAttribute("msg","请登录后查看");
        request.getRequestDispatcher("login.jsp").forward(request,response);
    }



    int pageNum = 1;
    int pageSize = 20;

    int totalNum = 0;
    int totalPageNum = 0;

    try {
        pageNum = Integer.parseInt(request.getParameter("pageNum"));
    } catch (Exception e) {
        pageNum = 1;
    }



    //4、创建sql语句（下达命令）
    String datasql = "SELECT * from t_user where 1=1 ";
    String countSql = "Select count(0) as count from t_user where 1=1";
    String sql = "";
    String sno = request.getParameter("sno");
    if (sno != null && sno.length() > 0) {
        sql += " and sno like '%" + sno + "%'";
    } else {
        sno = "";
    }
    String name = request.getParameter("name");
    if (!isEmpty(name)) {
        sql += " and name like '%" + name + "%'";
    } else {
        name = "";
    }
    String sex = request.getParameter("sex");
    if (!isEmpty(sex)) {
        sql += " and sex = '" + sex + "'";
    }else{
        sex="";
    }

    String phone = request.getParameter("phone");
    if (!isEmpty(phone)) {
        sql += " and phone like '%" + phone + "%'";
    } else {
        phone = "";
    }

    countSql += sql;
    sql += " order by sno DESC";

    sql += " limit " + (pageNum - 1) * pageSize + " ," + pageSize;

    datasql += sql;


    ResultSet rs = stmt.executeQuery(countSql);
    if (rs.next()) {
        totalNum = rs.getInt("count");
    }
    totalPageNum = totalNum % pageSize == 0 ? totalNum / pageSize : (totalNum / pageSize + 1);  //400条/20条每页=20页 401/20 =21


    //5、执行sql语句（执行命令拉货）
    rs = stmt.executeQuery(datasql);
    //6、处理结果集（卸货）
%>

<center>
    <br>
    <h2>
    欢迎你：<span style="color: red;">${sessionScope.name}</span>
    </h2>
    <br>
    <form action="index.jsp" method="post">
        学号：<input name="sno" id="sno" value="<%=sno%>"> 姓名:<input name="name" id="name" value="<%=name%>">
        性别:<select name="sex" id="sex">
        <option value="">==全部==</option>
        <option value="男">男</option>
        <option value="女">女</option>
    </select>
        手机:<input name="phone" id="phone" value="<%=phone%>">
        <input type="submit" value="查找">
        <input type="button" value="重置" onclick="clear();">

    </form>

    <a href="student.jsp?action=add">添加</a>
    <a href="javascript:deleteMany()">删除</a>


    <table border="1px" width="80%">
        <tr>
            <td><input type="checkbox" id="checkAll" onchange="checkAll()">全选</td>
            <td>学号</td>
            <td>姓名</td>
            <td>性别</td>
            <td>电话</td>
            <td>操作</td>
        </tr>
        <%
            while (rs.next()) {
                out.print("<tr>");
                out.print("<td><input class='checkIds' type=\"checkbox\" value=" + rs.getString("id") + "></td>");
                out.print("<td>" + rs.getString("sno") + "</td>");
                out.print("<td>" + rs.getString("name") + "</td>");
                out.print("<td>" + rs.getString("sex") + "</td>");
                out.print("<td>" + rs.getString("phone") + "</td>");
                out.print("<td><a href='student.jsp?action=view&id=" + rs.getString("id") + "'>查看</a>| <a href='student.jsp?action=edit&id=" + rs.getString("id") + "'>修改</a>| <a href='javascript:deleteOne(\"" + rs.getString("id") + "\")'>删除</a></td>");
                out.print("</tr>");
            }
            connection.close();
        %>
    </table>

    <%
        String query="&sno="+sno+"&name="+name+"&sex="+sex+"&phone="+phone;
    %>


    <a href="index.jsp?pageNumn=1<%=query%>">首页</a>|
    <%
        if (pageNum > 1) {
    %>
    <a href="index.jsp?pageNum=<%=pageNum-1<1?1:(pageNum-1)%><%=query%>">上一页</a>|
    <%
        }
    %>
    <%
        if (pageNum < totalPageNum) {
    %>
    <a href="index.jsp?pageNum=<%=pageNum+1>totalPageNum?totalPageNum:pageNum+1%><%=query%>">下一页</a>|
    <%
        }
    %>
    <a href="index.jsp?pageNum=<%=totalPageNum%><%=query%>">尾页</a><br>



    当前第<span><%=pageNum%></span>页，共<span><%=totalPageNum%></span>页，共<span><%=totalNum%></span>条记录



</center>
</body>
</html>
